package com.webdesign.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.webdesign.dto.MyPage;
import com.webdesign.dto.TermSubmitInfoDTO;
import com.webdesign.dto.TitleSubmitInfoDTO;
import com.webdesign.dto.UserHomeworkDTO;
import com.webdesign.entity.TbHomework;
import com.webdesign.entity.TbUser;
import com.webdesign.service.ITbUserhomeworkService;

@Controller("SumAction")
@Scope("prototype")
public class SumAction extends ActionSupport {
	private ITbUserhomeworkService iTbUserhomeworkService;

	private String homeworkid;
	private String score;
	private String userhomeworkid;
	private Boolean flag;
	private String numpage;
	private MyPage page;
	private List<TermSubmitInfoDTO> termsubmits;
	private List<UserHomeworkDTO> homeworklist;
	private List<TbUser> unfinish;
	

	/*
	 * 作业查看页面，导出全学年的作业完成情况的excel表格
	 */

	public String downExcel() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		String termid = (String) session.get("termid");
		String teacherid = (String) session.get("userid");
		flag = iTbUserhomeworkService.outputGrade(termid,teacherid);
		
		return SUCCESS;
	}
		/*
	 * 作业查看页面首先跳转到这个action，返回根据学年统计的结果
	 */
	public String TermSum() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		String termid = (String) session.get("termid");
		String userid = (String)session.get("userid");
		termsubmits = iTbUserhomeworkService.termSum(termid,userid);
		
		String pageNumber = CookieAction.getCookie("pagenumber");
		if(page == null){
			page = new MyPage();
			page.setNumber(1);
		}
		if(pageNumber != null && pageNumber!=""){
			page.setNumber(Integer.parseInt(pageNumber));
		}
		
		if(numpage != null){
			page.setNumber(Long.parseLong(numpage));
		}
		
		page.setTotalElements(termsubmits.size());
		termsubmits = termsubmits.subList((int)(page.getStart()), (int)(page.getEnd()));
		return SUCCESS;
	}
		/*
	 * 作业查看页面，点击完成人数，把homeworkid传过来，跳转到这个action，根据作业编号返回该题目已经完成的学生及作业信息
	 */
	public String showHomework() {
		homeworklist = iTbUserhomeworkService.showSubmit(homeworkid);
		
		return SUCCESS;
	}
		/*
	 * 作业查看页面，点击未完成人数，把homeworkid传过来，跳转到这个action，根据作业编号返回该题目未完成的学生信息
	 */
	public String showUnfinishHomework() {
		Map<String,Object> session = ActionContext.getContext().getSession();
		String termid = (String)session.get("termid");
		String userid = (String)session.get("userid");
		unfinish = iTbUserhomeworkService.showUnsubmit(homeworkid,termid,userid);
		
		String pageNumber = CookieAction.getCookie("pagenumber");
		if(page == null){
			page = new MyPage();
			page.setNumber(1);
		}
		if(pageNumber != null && pageNumber!=""){
			page.setNumber(Integer.parseInt(pageNumber));
		}
		
		if(numpage != null){
			page.setNumber(Long.parseLong(numpage));
		}
		
		page.setTotalElements(unfinish.size());
		unfinish = unfinish.subList((int)(page.getStart()), (int)(page.getEnd()));
		return SUCCESS;
	}
		/*
	 * （12.14 clover）作业评分
	 */
	public String grade(){
		flag = iTbUserhomeworkService.grade(userhomeworkid, score);
		return SUCCESS;
	}
	
	public ITbUserhomeworkService getiTbUserhomeworkService() {
		return iTbUserhomeworkService;
	}

	@Resource(name = "TbUserhomeworkService")
	public void setiTbUserhomeworkService(
			ITbUserhomeworkService iTbUserhomeworkService) {
		this.iTbUserhomeworkService = iTbUserhomeworkService;
	}

	public String getHomeworkid() {
		return homeworkid;
	}

	public void setHomeworkid(String homeworkid) {
		this.homeworkid = homeworkid;
	}
		public String getScore() {
		return score;
	}

	public void setScore(String score) {
		this.score = score;
	}

	public String getUserhomeworkid() {
		return userhomeworkid;
	}

	public void setUserhomeworkid(String userhomeworkid) {
		this.userhomeworkid = userhomeworkid;
	}

	public Boolean getFlag() {
		return flag;
	}

	public void setFlag(Boolean flag) {
		this.flag = flag;
	}
	
	public String getNumpage() {
		return numpage;
	}
	
	public void setNumpage(String numpage) {
		this.numpage = numpage;
	}
	public MyPage getPage() {
		return page;
	}
	public void setPage(MyPage page) {
		this.page = page;
	}
	public List<TermSubmitInfoDTO> getTermsubmits() {
		return termsubmits;
	}
	public void setTermsubmits(List<TermSubmitInfoDTO> termsubmits) {
		this.termsubmits = termsubmits;
	}
	public List<UserHomeworkDTO> getHomeworklist() {
		return homeworklist;
	}
	public void setHomeworklist(List<UserHomeworkDTO> homeworklist) {
		this.homeworklist = homeworklist;
	}
	public List<TbUser> getUnfinish() {
		return unfinish;
	}
	public void setUnfinish(List<TbUser> unfinish) {
		this.unfinish = unfinish;
	}
	
	

}
